﻿using UnityEngine;
using System.Collections;

/// <summary>
/// Manage Level(Scene) Class.
/// Usage...
/// Load my level - wsLevel.FadeAndLoadLevel("mylevel", Color.white, 0.5f);
/// Reset the current level - wsLevel.FadeAndLoadLevel(Application.loadedLevel, Color.white, 0.5f);
/// </summary>
[AddComponentMenu("wsLibrary/Util/Load Level(Scene)")]
public class wsLoadLevel : MonoBehaviour
{
	/// <summary>
	/// fade and load level using texture that you aleady have.
	/// </summary>
	/// <param name="level">level name</param>
	/// <param name="fadeTexture">texture for fade</param>
	/// <param name="fadeLength">time for fade</param>
	static public void FadeAndLoadLevel(string level, Texture2D fadeTexture, float fadeLength)
	{
		if (fadeTexture == null)
			FadeAndLoadLevel(level, Color.white, fadeLength);

		var fade = new GameObject("Fade");
		fade.AddComponent<wsLoadLevel>();
		fade.AddComponent<GUITexture>();
		fade.transform.position = new Vector3(0.5f, 0.5f, 1000);
		fade.guiTexture.texture = fadeTexture;
		wsLoadLevel lf = fade.GetComponent<wsLoadLevel>() as wsLoadLevel;
		lf.StartCoroutine(lf.DoFade(level, fadeLength, false));
	}

	/// <summary>
	/// fade and load level using color - texture is create automatically.
	/// </summary>
	/// <param name="level">level name</param>
	/// <param name="color">texture for fade</param>
	/// <param name="fadeLength">time for fade</param>
	static public void FadeAndLoadLevel(string level, Color color, float fadeLength)
	{
		var fadeTexture = new Texture2D(1, 1);
		fadeTexture.SetPixel(0, 0, color);
		fadeTexture.Apply();

		var fade = new GameObject("Fade");
		fade.AddComponent<wsLoadLevel>();
		fade.AddComponent<GUITexture>();
		fade.transform.position = new Vector3(0.5f, 0.5f, 1000);
		fade.guiTexture.texture = fadeTexture;

		DontDestroyOnLoad(fadeTexture);
		wsLoadLevel lf = fade.GetComponent<wsLoadLevel>();
		lf.StartCoroutine(lf.DoFade(level, fadeLength, true));
	}

	/// <summary>
	/// main method for fade and load level
	/// </summary>
	/// <param name="level">level name</param>
	/// <param name="fadeLength">time for fade</param>
	/// <param name="destroyTexture">destroy texture ?</param>
	/// <returns></returns>
	IEnumerator DoFade(string level, float fadeLength, bool destroyTexture)
	{
		// Dont destroy the fade game object during level load
		DontDestroyOnLoad(gameObject);

		// Fadeout to start with
		Color color = guiTexture.color;
		color.a = 0.0f;
		guiTexture.color = color;

		// Fade texture in
		var time = 0.0f;
		while (time < fadeLength)
		{
			time += Time.deltaTime;
			color = guiTexture.color;
			color.a = Mathf.InverseLerp(0.0f, fadeLength, time);
			guiTexture.color = color;
			yield return null;
		}
		color = guiTexture.color;
		color.a = 1.0f;
		guiTexture.color = color;
		yield return null;

		// Complete the fade out (Load a level or reset player position)
		Application.LoadLevel(level);

		// Fade texture out
		time = 0.0f;
		while (time < fadeLength)
		{
			time += Time.deltaTime;
			color = guiTexture.color;
			color.a = Mathf.InverseLerp(fadeLength, 0.0f, time);
			guiTexture.color = color;
			yield return null;
		}
		color = guiTexture.color;
		color.a = 0.0f;
		guiTexture.color = color;
		yield return null;

		Destroy(gameObject);

		// If we created the texture from code we used DontDestroyOnLoad,
		// which means we have to clean it up manually to avoid leaks
		if (destroyTexture)
			Destroy(guiTexture.texture);
	}
}